package com.feib.soeasy.action;

import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;

import com.feib.soeasy.model.CodeTable;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.Payment;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.CodeTableService;
import com.feib.soeasy.service.GroupService;
import com.feib.soeasy.service.PaymentService;

/**
 * @title (#)Soez1003ActionBean.java <br>
 * @description 學雜費繳費進度查詢 Controller<br>
 * @author Leo Lee
 * @version 1.0.0 2010/11/28
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/28 create by Leo Lee<br>
 */
public class Soez1003ActionBean extends BaseActionBean implements ValidationErrorHandler {

    /**
     * 學雜費繳費作業畫面
     */
    private static final String DEFAULT_JSP = "/WEB-INF/views/soez1003/soez1003.jsp";

    /**
     * 學雜費繳費清單畫面
     */
    private static final String PAYMENT_LIST_JSP = "/WEB-INF/views/soez1003/soez1003List.jsp";
    /**
     * 檔案內容
     */
    private static final String AJAX_QUERY_PAYMENT_JSP = "/WEB-INF/views/soez1003/paymentList.jsp";


    private String groupNo;
    private String queryId;
    private String queryBirthday;//20110602 黃郁珊 增加增加生日檢核
    private Long paymentPk;

    @SpringBean("paymentService")
    private PaymentService paymentService;

    @SpringBean("groupService")
    private GroupService groupService;

    @SpringBean("codeTableService")
    private CodeTableService codeTableService;


    /**
     * 查詢結果
     */
    private PaginatedList<Payment> queryResult;
    /**
     * 查詢單筆Payment
     */
    private Payment resultPayment;
    
    /**
     * 通路代碼
     */
    private List<CodeTable> channelCodes;


    @DontBind
    @DefaultHandler
    public Resolution doDefault()
    {       
        logger.debug("Enter doDefault mehtod.");
        return new ForwardResolution(DEFAULT_JSP); 
    }

    /**
     * 查詢
     * @return
     */
	public Resolution doAjaxQuery() {
		logger.debug("Enter doAjaxQuery mehtod.");
		try {
			if (groupNo == null || queryId == null || queryBirthday == null) {
				queryResult.setMessage("不可輸入空白!");
			} else {
				Payment qryPayment = new Payment();
				Group group = new Group();
				group.setGroupNo(groupNo);
				qryPayment.setGroup(group);

				queryResult = paymentService.queryByPayment(qryPayment, null,
						null, queryResult, "Q");

				if (queryResult.getFullListSize() > 0) {
					qryPayment.setStudentId(queryId);
					queryResult = paymentService.queryByPayment(qryPayment,
							null, null, queryResult, "Q");
					if (queryResult.getFullListSize() > 0) {
						qryPayment.setPayerBirthday(queryBirthday);
						 queryResult.setSortCriterion("virtualAccount");
				            queryResult.setSortDirection(1);           
				            queryResult.setPageSize(999);
						queryResult = paymentService.queryByPayment(qryPayment,
								null, null, queryResult, "Q");
						if (queryResult.getFullListSize() == 0) {
							queryResult.setMessage("生日錯誤");
						}
					} else {
						queryResult.setMessage("學號錯誤");
					}
				} else {
					queryResult.setMessage("查無資料");
				}
			}
		} catch (Exception e) {
			logger.error("學雜費繳費查詢錯誤(Soez1003ActionBean.doAjaxQuery):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new LocalizableError("doAjaxQuery.exception"));
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(PAYMENT_LIST_JSP);
	}

    /**
     * 查詢轉入的帳單資料
     * @return
     */
    public Resolution doAjaxQueryFilePayments()
    {
        logger.debug("Enter doAjaxQueryFilePayments mehtod.");
        try {

            //呼叫服務查詢
            Payment qryPayment = new Payment();
            qryPayment.setPaymentPk(paymentPk);
            resultPayment = 
                paymentService.getPaymentByPk(qryPayment);
        }
        catch (Exception e) {
            logger.error("上傳記錄查詢-帳單內容查詢錯誤(Soez1017ActionBean.doAjaxQueryFilePayments):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(AJAX_QUERY_PAYMENT_JSP);
    }


    public PaginatedList<Payment> getQueryResult() {
        return queryResult;
    }

    public void setQueryResult(PaginatedList<Payment> queryResult) {
        this.queryResult = queryResult;
    }


    public String getGroupNo() {
        return groupNo;
    }

    public void setGroupNo(String groupNo) {
        this.groupNo = groupNo;
    }

    public String getQueryId() {
        return queryId;
    }

    public void setQueryId(String queryId) {
        this.queryId = queryId;
    }

    public Long getPaymentPk() {
        return paymentPk;
    }

    public void setPaymentPk(Long paymentPk) {
        this.paymentPk = paymentPk;
    }

    public Payment getResultPayment() {
        return resultPayment;
    }

    public void setResultPayment(Payment resultPayment) {
        this.resultPayment = resultPayment;
    }
        
    /**
     * @return the channelCodes
     */
    public List<CodeTable> getChannelCodes() {
        if (null == channelCodes)
        {
            channelCodes = codeTableService.queryByCodeKind("CHANEL");
        }
        return channelCodes;
    }
    
    public String getQueryBirthday() {
		return queryBirthday;
	}

	public void setQueryBirthday(String queryBirthday) {
		this.queryBirthday = queryBirthday;
	}

	public String decodeChannel(String code)
    {
        String res = "";
        
        for (CodeTable codeTable : getChannelCodes())
        {
            if (codeTable.getCodeValue().equalsIgnoreCase(code))
                return codeTable.getCodeName();            
        }
        
        return res;
    }


}
